package main

import (
	"image"
	"sync"
)

// 读写互斥锁（读 > 写的场景）
var (
	icons         map[string]image.Image
	loadIconsOnce sync.Once
)

func loadIcons() {
	icons = map[string]image.Image{
		"left":   loadIcon("left.png"),
		"center": loadIcon("center.png"),
		"right":  loadIcon("right.png"),
		"down":   loadIcon("down.png"),
	}
}

// Icon被多个goroutine调用时不是并发安全的,使用once保证并发安全
func Icon(name string) image.Image {
	// if icons == nil {
	// 	loadIcons()
	// }
	loadIconsOnce.Do(loadIcons)
	return icons[name]
}

func main() {

}
